package com.maybach.core.ao.impl;

import com.maybach.common.enums.order.InfoTypeEnum;
import com.maybach.common.model.LoanOrderDO;
import com.maybach.common.model.ShippingOrderDO;
import com.maybach.common.model.UserDO;
import com.maybach.common.model.UserMsgDO;
import com.maybach.core.ao.UserMsgAO;
import com.maybach.core.manager.UserManager;
import com.maybach.core.manager.UserMsgManager;
import com.maybach.core.msg.IMsgService;
import com.maybach.core.template.impl.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by houenxun on 16/3/7.
 */
@Component
public class UserMsgAOImpl implements UserMsgAO {
    @Autowired
    TemplateService templateService;
    @Autowired
    UserMsgManager userMsgManager;
    @Autowired
    IMsgService msgService;

    @Autowired
    UserManager userManager;


    @Override
    public void closeOrder(LoanOrderDO loanOrderDO, String reason) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("loanOrder", loanOrderDO);
        map.put("reason", reason);

        sendMsg(loanOrderDO, map, "closeOrder");
    }

    @Override
    public void confirmOrder(LoanOrderDO loanOrderDO) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("loanOrder", loanOrderDO);

        sendMsg(loanOrderDO, map, "confirmOrder");

    }

    @Override
    public void passInfo(LoanOrderDO loanOrderDO, InfoTypeEnum type) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("loanOrder", loanOrderDO);
        map.put("type", type.getDesc());

        sendMsg(loanOrderDO, map, "infoPass");

    }

    @Override
    public void rejectInfo(LoanOrderDO loanOrderDO, InfoTypeEnum type, String reason) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("loanOrder", loanOrderDO);
        map.put("type", type.getDesc());
        map.put("reason", reason);
        sendMsg(loanOrderDO, map, "infoReject");
    }

    private void sendMsg(LoanOrderDO loanOrderDO, Map<String, Object> map, String target) {
        String body = templateService.getTempateBody(target, map);
        String title = templateService.getTemplateTitle(target);
        this.sendUserMsg(loanOrderDO,title, body, "viewOrder");
    }

    private void sendUserMsg(LoanOrderDO loanOrderDO, String title,String body, String action) {
        UserMsgDO userMsgDO = new UserMsgDO();
        userMsgDO.setOrderId(loanOrderDO.getId());
        userMsgDO.setOrderNo(loanOrderDO.getOrderNo());
        userMsgDO.setOrderName(loanOrderDO.getOrderName());
        userMsgDO.setUserId(loanOrderDO.getBuyerId());

        userMsgDO.setAction(action);
        userMsgDO.setContent(body);
        userMsgDO.setTitle(title);
        userMsgDO.setMsgType("orderMsg");
        userMsgManager.insertSelective(userMsgDO);
        UserDO userDO = userManager.selectByPrimaryKey(loanOrderDO.getBuyerId());
        if(userDO!=null){
            msgService.sendMsg(userDO.getLoginName(), body + "【卖好车】");
        }
    }

    @Override
    public void shipping(LoanOrderDO loanOrderDO, ShippingOrderDO shippingOrderDO) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("loanOrder", loanOrderDO);
        map.put("shippingOrder", shippingOrderDO);
        sendMsg(loanOrderDO, map, "shipping");
    }

    @Override
    public void payToProvider(LoanOrderDO loanOrderDO) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("loanOrder", loanOrderDO);

        sendMsg(loanOrderDO, map, "payToProvider");
    }
}
